<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户登录/注册</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script>
    tailwind.config = {
      theme: {
        extend: {
          colors: {
            primary: '#165DFF',
            secondary: '#4080FF',
            neutral: '#F2F3F5',
            dark: '#1D2129',
          },
          fontFamily: {
            inter: ['Inter', 'system-ui', 'sans-serif'],
          },
        },
      }
    }
  </script>
  <style type="text/tailwindcss">
    @layer utilities {
      .content-auto {
        content-visibility: auto;
      }
      .bg-gradient-blue {
        background: linear-gradient(135deg, #165DFF 0%, #4080FF 100%);
      }
      .shadow-blue {
        box-shadow: 0 10px 25px -5px rgba(22, 93, 255, 0.1), 0 8px 10px -6px rgba(22, 93, 255, 0.1);
      }
      .input-focus {
        @apply focus:ring-2 focus:ring-primary/50 focus:border-primary focus:outline-none;
      }
      .form-transition {
        transition: all 0.3s ease-in-out;
      }
    }
  </style>
</head>
<body class="font-inter bg-gray-50 min-h-screen flex items-center justify-center p-4">
  <div class="w-full max-w-md">
    <div class="bg-white rounded-2xl shadow-blue overflow-hidden transition-all duration-300 hover:shadow-lg">
      <div class="bg-gradient-blue p-6 text-white">
        <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold mb-1" id="formTitle">欢迎回来</h2>
        <p class="text-white/80" id="formSubtitle">请登录您的账户</p>
      </div>
      
      <div class="p-6">
        <!-- 登录表单 -->
        <form id="loginForm" class="space-y-4 form-transition">
          <div class="relative">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
              <i class="fa fa-envelope-o text-gray-400"></i>
            </div>
            <input type="email" id="loginEmail" name="email" 
              class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 placeholder-gray-500 input-focus transition-all duration-300"
              placeholder="您的邮箱" required>
          </div>
          
          <div class="relative">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
              <i class="fa fa-lock text-gray-400"></i>
            </div>
            <input type="password" id="loginPassword" name="password" 
              class="w-full pl-10 pr-10 py-3 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 placeholder-gray-500 input-focus transition-all duration-300"
              placeholder="您的密码" required>
            <button type="button" id="toggleLoginPassword" class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-400 hover:text-gray-600 transition-colors">
              <i class="fa fa-eye-slash"></i>
            </button>
          </div>
          
          <div class="flex items-center justify-between">
            <div class="flex items-center">
              <input id="remember" type="checkbox" class="h-4 w-4 text-primary border-gray-300 rounded focus:ring-primary/50">
              <label for="remember" class="ml-2 block text-sm text-gray-700">记住我</label>
            </div>
            <a href="#" class="text-sm font-medium text-primary hover:text-primary/80 transition-colors">忘记密码?</a>
          </div>
          
          <button type="submit" 
            class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg transition-all duration-300 transform hover:scale-[1.02] focus:outline-none focus:ring-2 focus:ring-primary/50 focus:ring-offset-2">
            登录
          </button>
        </form>
        
        <!-- 注册表单 -->
        <form id="registerForm" class="space-y-4 form-transition hidden">
          <div class="relative">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
              <i class="fa fa-envelope-o text-gray-400"></i>
            </div>
            <input type="email" id="registerEmail" name="email" 
              class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 placeholder-gray-500 input-focus transition-all duration-300"
              placeholder="您的邮箱" required>
          </div>
          
          <div class="relative">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
              <i class="fa fa-lock text-gray-400"></i>
            </div>
            <input type="password" id="registerPassword" name="password" 
              class="w-full pl-10 pr-10 py-3 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 placeholder-gray-500 input-focus transition-all duration-300"
              placeholder="设置密码（至少8位）" required>
            <button type="button" id="toggleRegisterPassword" class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-400 hover:text-gray-600 transition-colors">
              <i class="fa fa-eye-slash"></i>
            </button>
          </div>
          
          <div class="relative">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
              <i class="fa fa-lock text-gray-400"></i>
            </div>
            <input type="password" id="registerConfirmPassword" name="confirmPassword" 
              class="w-full pl-10 pr-10 py-3 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 placeholder-gray-500 input-focus transition-all duration-300"
              placeholder="确认密码" required>
            <button type="button" id="toggleRegisterConfirmPassword" class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-400 hover:text-gray-600 transition-colors">
              <i class="fa fa-eye-slash"></i>
            </button>
          </div>
          
          <button type="submit" 
            class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg transition-all duration-300 transform hover:scale-[1.02] focus:outline-none focus:ring-2 focus:ring-primary/50 focus:ring-offset-2">
            注册
          </button>
        </form>
        
        <div class="mt-6 text-center">
          <p class="text-gray-600" id="toggleText">
            还没有账户? <a href="#" id="toggleToRegister" class="font-medium text-primary hover:text-primary/80 transition-colors">立即注册</a>
          </p>
        </div>
      </div>
    </div>
    
    <div id="message" class="mt-4 hidden">
      <div class="p-4 rounded-lg transition-all duration-300 transform opacity-0 translate-y-2" id="messageContent"></div>
    </div>
  </div>

  <script>
    // DOM元素
    const loginForm = document.getElementById('loginForm');
    const registerForm = document.getElementById('registerForm');
    const formTitle = document.getElementById('formTitle');
    const formSubtitle = document.getElementById('formSubtitle');
    const toggleText = document.getElementById('toggleText');
    const toggleToRegister = document.getElementById('toggleToRegister');
    const toggleToLogin = document.createElement('a');
    toggleToLogin.className = 'font-medium text-primary hover:text-primary/80 transition-colors';
    toggleToLogin.textContent = '已有账户? 登录';
    
    const message = document.getElementById('message');
    const messageContent = document.getElementById('messageContent');
    
    // 切换登录/注册表单
    function showLoginForm() {
      registerForm.classList.add('hidden');
      loginForm.classList.remove('hidden');
      formTitle.textContent = '欢迎回来';
      formSubtitle.textContent = '请登录您的账户';
      toggleText.innerHTML = '还没有账户? <a href="#" id="toggleToRegister" class="font-medium text-primary hover:text-primary/80 transition-colors">立即注册</a>';
      document.getElementById('toggleToRegister').addEventListener('click', toggleForms);
    }
    
    function showRegisterForm() {
      loginForm.classList.add('hidden');
      registerForm.classList.remove('hidden');
      formTitle.textContent = '创建账户';
      formSubtitle.textContent = '请填写信息完成注册';
      toggleText.innerHTML = '已有账户? <a href="#" id="toggleToLogin" class="font-medium text-primary hover:text-primary/80 transition-colors">登录</a>';
      document.getElementById('toggleToLogin').addEventListener('click', toggleForms);
    }
    
    function toggleForms(e) {
      if (e) e.preventDefault();
      if (loginForm.classList.contains('hidden')) {
        showLoginForm();
      } else {
        showRegisterForm();
      }
    }
    
    // 初始设置切换按钮事件
    document.getElementById('toggleToRegister').addEventListener('click', toggleForms);
    
    // 密码显示/隐藏切换功能
    function setupPasswordToggle(toggleBtnId, passwordInputId) {
      const toggleBtn = document.getElementById(toggleBtnId);
      const passwordInput = document.getElementById(passwordInputId);
      
      toggleBtn.addEventListener('click', function() {
        const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        passwordInput.setAttribute('type', type);
        this.querySelector('i').classList.toggle('fa-eye');
        this.querySelector('i').classList.toggle('fa-eye-slash');
      });
    }
    
    // 为登录和注册表单设置密码切换
    setupPasswordToggle('toggleLoginPassword', 'loginPassword');
    setupPasswordToggle('toggleRegisterPassword', 'registerPassword');
    setupPasswordToggle('toggleRegisterConfirmPassword', 'registerConfirmPassword');
    
    // 显示消息函数
    function showMessage(text, type) {
      message.classList.remove('hidden');
      messageContent.textContent = text;
      
      // 移除所有可能的类
      messageContent.classList.remove('bg-green-50', 'border-green-500', 'text-green-800', 
                                     'bg-red-50', 'border-red-500', 'text-red-800',
                                     'border-l-4');
      
      // 添加适当的类
      if (type === 'success') {
        messageContent.classList.add('bg-green-50', 'border-l-4', 'border-green-500', 'text-green-800');
      } else {
        messageContent.classList.add('bg-red-50', 'border-l-4', 'border-red-500', 'text-red-800');
      }
      
      // 显示动画
      setTimeout(() => {
        messageContent.classList.remove('opacity-0', 'translate-y-2');
      }, 10);
      
      // 3秒后隐藏消息
      setTimeout(() => {
        messageContent.classList.add('opacity-0', 'translate-y-2');
        setTimeout(() => {
          message.classList.add('hidden');
        }, 300);
      }, 3000);
    }
    
    // 登录表单提交
    loginForm.addEventListener('submit', async function(e) {
      e.preventDefault();
      
      // 显示加载状态
      const submitButton = this.querySelector('button[type="submit"]');
      const originalText = submitButton.innerHTML;
      submitButton.disabled = true;
      submitButton.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i> 登录中...';
      
      try {
        // 收集表单数据
        const formData = {
          email: document.getElementById('loginEmail').value,
          password: document.getElementById('loginPassword').value
        };
        
        // 发送登录请求
        const response = await fetch('/api/login', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(formData)
        });
        
        const data = await response.json();
        
        // 显示消息
        showMessage(data.message, response.ok ? 'success' : 'error');
        
        if (response.ok) {
          // 登录成功，重定向到主页或其他页面
          setTimeout(() => {
            window.location.href = '/html/book.html';

          }, 1500);
        }
      } catch (error) {
        showMessage('网络错误，请稍后重试', 'error');
        console.error('登录错误:', error);
      } finally {
        // 恢复按钮状态
        submitButton.disabled = false;
        submitButton.innerHTML = originalText;
      }
    });
    
    // 注册表单提交
    registerForm.addEventListener('submit', async function(e) {
      e.preventDefault();
      
      // 获取表单值
      const email = document.getElementById('registerEmail').value;
      const password = document.getElementById('registerPassword').value;
      const confirmPassword = document.getElementById('registerConfirmPassword').value;
      
      // 前端验证
      if (password !== confirmPassword) {
        showMessage('两次输入的密码不一致', 'error');
        return;
      }
      
      if (password.length < 8) {
        showMessage('密码长度至少为8个字符', 'error');
        return;
      }
      
      // 邮箱格式验证
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!emailRegex.test(email)) {
        showMessage('请输入有效的邮箱地址', 'error');
        return;
      }
      
      // 显示加载状态
      const submitButton = this.querySelector('button[type="submit"]');
      const originalText = submitButton.innerHTML;
      submitButton.disabled = true;
      submitButton.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i> 注册中...';
      
      try {
        // 收集表单数据
        const formData = {
          email: email,
          password: password
        };
        
        // 发送注册请求
        const response = await fetch('/api/register', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(formData)
        });
        
        const data = await response.json();
        
        // 显示消息
        showMessage(data.message, response.ok ? 'success' : 'error');
        
        if (response.ok) {
          // 注册成功后切换到登录表单
          setTimeout(() => {
            showLoginForm();
            // 填充邮箱
            document.getElementById('loginEmail').value = email;
          }, 1500);
        }
      } catch (error) {
        showMessage('网络错误，请稍后重试', 'error');
        console.error('注册错误:', error);
      } finally {
        // 恢复按钮状态
        submitButton.disabled = false;
        submitButton.innerHTML = originalText;
      }
    });
  </script>
</body>
</html>